<?php
namespace app\common;
use think\Model;
use think\Db;

class TeacherModel extends Model{
    
	public function getClassTeacherList($page, $rows, $where){
        $result = Db::table('班主任科任')->alias("a")
                  ->join('教师信息 b','a.班主任ID=b.ID','LEFT')
                  ->field("a.ID,a.年级号,b.学籍类型,班号,班主任ID,b.姓名 班主任,语文,数学,英语,政治,历史,体育,物理,化学,地理,生物")
                  ->order("学籍类型, 年级号 desc, 班号")
                  ->where($where)
                  ->page($page, $rows)
                  ->select();
            $count = Db::table("班主任科任 a")->where($where)->count();
            $grid['rows'] = $this::_get_result(selectResult($result));
            $grid['total'] = $count;
            return $grid;
	}

    public function getTeacherList($page, $rows, $fields, $where, $orderby=null){
        $result = Db::table("教师信息")
                ->field($fields)
                ->where($where)
                ->page($page, $rows)
                ->order($orderby ? $orderby : "学籍类型,年级号 desc")
                ->select();
        $count = Db::table("教师信息")->where($where)->count();
        $grid['rows'] = $this::_get_result(selectResult($result));
        $grid['total'] = $count;
        return $grid;
    }

    static protected function _get_result($data) {
        $list = [];
        foreach($data as $key => $obj){
            $result = $obj;
            foreach($obj as $key2 => $value){
                if($key2 == 'grade' || $key2 == '年级号') {
                    $result['gradeName'] = getCommonGradeName($value,$obj["学籍类型"]);
                    if($value != '0') {
                        $result['年级号'] = $value."_".$obj["学籍类型"];
                    }
                }else if ($key2 == '任教学科ID') {
                    $result['subjectName'] = getSubjectName($value);
                }else if($key2 == '职位') {
                    $result["positionText"]  = getPositionName($value);
                }else if($key2 == '部门') {
                    $result["departText"]  = getDepartName($value);
                }
            }
            $list[$key] = $result;
        }
        return $list;
    }
}
